package cn.shengongying.api.controller;

import cn.shengongying.anon.User;
import cn.shengongying.bean.BaseUser;
import cn.shengongying.bean.ResponseResult;
import cn.shengongying.model.UserDiscountInfo;
import cn.shengongying.service.IUserDiscountInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : RXK
 * Date : 2021-7-24 18:02
 * Code Less Think More
 * Desc:
 */
@RestController
@Api(tags = "优惠券")
@RequestMapping("/boss/discount")
public class DiscountController extends BaseController {


    private final IUserDiscountInfoService discountInfoService;

    public DiscountController(IUserDiscountInfoService discountInfoService) {
        this.discountInfoService = discountInfoService;
    }

    @ApiOperation(value = "优惠券列表")
    @PostMapping("/list")
    public ResponseResult<List<UserDiscountInfo>> list(@ApiIgnore @User BaseUser baseUser) {
        List<UserDiscountInfo> infoList = discountInfoService.findByUserId(baseUser.getId());
        infoList = infoList.stream()
                .sorted(Comparator.comparing(UserDiscountInfo::getStatus)
                        .thenComparing(UserDiscountInfo::getMoney)
                        .reversed()
                        .thenComparing(UserDiscountInfo::getUpdateDate)
                        .reversed())
                .collect(Collectors.toList());
        return ResponseResult.success(infoList);
    }


}
